package com.c1g19.action.office;

import com.c1g19.action.BaseAction;
import com.c1g19.service.office.OfficeService;
import com.c1g19.utils.Struts2Utils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * 用以处理科室页面的Action
 * Created by Mr_C on 2017/3/14.
 */
public class OfficeAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(OfficeAction.class);

    int office_id = 0;

    /**
     * getter and setter
     */
    public int getOffice_id() {
        return office_id;
    }

    public void setOffice_id(int office_id) {
        this.office_id = office_id;
    }

    /**
     * 跳转到诊室页
     * @return
     */
    public String preViewOffice(){
        return "success";
    }

    /**
     * 跳转诊室详情页
     * @return
     */
    public String preOfficeDetail(){
        getRequest().setAttribute("office_id", office_id);
        return "success";
    }

    /**
     * 得到所有的诊室
     */
    public void getAllOffice(){
        OfficeService officeService = new OfficeService();
        Map<String, Object> allOfficeList = officeService.getAllOffice();

        //发给网页执行操作
        String resultJsonString = changeListToJsonStringWithJtable(allOfficeList);
        Struts2Utils.renderJson(resultJsonString);
    }


    /**
     * 获得科室详细信息
     */
    public void getOfficeDetail(){
        logger.info("开始查询相关科室，科室ID为:"+office_id);
        OfficeService officeService = new OfficeService();
        Map<String, Object> officeDetailList = officeService.getOfficeDetail(office_id);
        //发给网页执行操作
        JSONObject jsonObject = new JSONObject();
        try {

            for (String key : officeDetailList.keySet()) {
                if((key.contains("_loc")||key.equals("relevant_person"))&&!key.contains("_count")) {
                    List<String> loclist = (List<String>) officeDetailList.get(key);
                    JSONArray locjsonarray = new JSONArray(loclist);
                    jsonObject.put(key, locjsonarray);
                }else{
                    jsonObject.put(key, officeDetailList.get(key).toString());
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        String resultJsonString = jsonObject.toString();
        Struts2Utils.renderJson(resultJsonString);
        logger.info("officedetailfinished");
    }
}
